<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>圣米庐 | 修改用户信息</title>
        <meta content='width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no' name='viewport'>
        <!-- bootstrap 3.0.2 -->
        <link href="css/bootstrap-3.2.0/css/bootstrap.css" rel="stylesheet" type="text/css" />
        <link href="css/bootstrap-box.css" rel="stylesheet" type="text/css" />
        <link href="css/bootstrap-page.css" rel="stylesheet" type="text/css" />
        <link href="js/validator/jquery.validator.css" rel="stylesheet" type="text/css" />
        <script type="text/javascript" src="js/jquery-1.11.0.min.js"></script>
        <script type="text/javascript" src="js/bootstrap.min.js"></script>
        <script type="text/javascript" src="js/jsviews.js"></script>
        <script type="text/javascript" src="js/loadTmpl.js"></script>
        <script	type="text/javascript" src="js/validator/jquery.validator.js"></script>
		<script type="text/javascript" src="js/validator/local/zh_CN.js"></script>
        <script type="text/javascript">
        	//获取浏览器参数
        	function getUrlParam(name){
        		var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
        		var r = window.location.search.substr(1).match(reg);  //匹配目标参数
        		if (r!=null) return unescape(r[2]); return null; //返回参数值
        	}
        	$.views.converters("ischecked",function(first,last){
        		var flag=false;
        		for(var i=0;i<last.length;i++){
        			var _rid=last[i].roleId;
        			if(first ==_rid){
        				flag=true;
	        			break;
        			} 
        		}
    			return flag? "checked=checked":"";
    		});
			$(function(){
				var userId=getUrlParam("userId");
				if(userId==""){
					alert("userId参数不能为空");
					return;
				}
				$("#userId").val(userId);
				var url ='xaCmsUser/'+userId;
				$.ajax({
					url:url,
					type:'get',
					dataType:'json',
					success:function(data){
						var userContentHtml=$("#userTmpl").render(data);
						$("#usercontent").html(userContentHtml);
					}
				});
				$("#saveOrupdate").click(function(){
					$('#demo_31').isValid(function(result){
					    if(!result){
							alert("还有未填写的字段，请检查！");
							return;
					    }
					    var userId=$("#userId").val();
						var selectArray =new Array();	//下拉选中项
						$("input[name='roleCkBox']:checked").each(function () {
							selectArray.push($(this).val());
		                });
						var userName=$("#userName").val();
						var realName=$("#realName").val();
						var password=$("#password").val();
						var mobile=$("#mobile").val();
						var email=$("#email").val();
						var description=$("#description").val();
						var selectRole=""+selectArray+"";
						var xaCmsUser="";
						var url="";
						var ajaxType="";
						if(typeof(userId)=="undefined"){
							//新增
							xaCmsUser={"userName":userName,"realName":realName,"password":password,"mobile":mobile,"email":email,"description":description};
							url="xaCmsUser/addUser/"+selectRole;
							ajaxType="POST";
						}else{
							//修改
							xaCmsUser={"userId":userId,"userName":userName,"realName":realName,"mobile":mobile,"email":email,"description":description};
							url="xaCmsUser/update/"+selectRole;
							ajaxType="POST";
						}
						$.ajax({
							url:url,
							type:ajaxType,
							datType:'json',
							contentType: "application/json",
							data:JSON.stringify(xaCmsUser),
							success:function(data){
								if(data.status=1){
									window.location.href="userList.html";
								}
							}
						});
					});
				});
			});
        </script>
    </head>
    <body>
      <section>
          <div>
              <div class="col-xs-12">
                  <div class="box">
                      <div class="box-header">
                      <br>
                          <input type="button" value="确定" id="saveOrupdate" class="btn btn-success">
                          <a  href="userList.html" class="btn btn-info" >返回</a>
                      </div>
                     <form id="demo_31" >
                      <div class="box-body table-responsive" id="usercontent">
                  	</div>
                  	</form>
              </div>
          </div>
      </section>
      <script type="text/x-jsrender" id="userTmpl">
		{{if user}}
			<input type="hidden" id="userId" value="{{:user.userId}}" />
	        <span>用户名:</span><input type="text" id="userName" readonly="readonly" value="{{:user.userName}}" /><br /><br/>
	        <span>真实姓名:</span><input type="text" id="realName" data-rule="required;realName;length[5~30]" value="{{:user.realName}}" /><br /><br/>
	        <span>手机号:</span><input type="text" id="mobile" data-rule="手机号:required;length[11]"  value="{{:user.mobile}}" /><br /><br/>
	        <span>email:</span><input type="text" id="email" data-rule="required;email;length[5~]"  value="{{:user.email}}" /><br /><br/>
	        <span>描述:</span><textarea id="description" rows="3" data-rule="描述:required;length[1~, true]" cols="50">{{:user.description}}</textarea><br/><br/>
			<span>角色:</span>
			{{for role}}
				<input type="checkbox" data-rule="checked" id="roleCk_{{:roleId}}" value="{{:roleId}}" name="roleCkBox" {{ischecked:roleId #parent.parent.data.userRole}} />{{:roleName}}
			{{/for}}
		{{else}}
	        <span>用户名:</span><input type="text" name="userName" data-rule="required;userName;length[5~30];remote[get:xaCmsUser/checkUserName]" id="userName" /><br /><br/>
	        <span>真实姓名:</span><input type="text" data-rule="required;realName;length[2~30]" id="realName" /><br /><br/>
	        <span>密码:</span><input type="password" data-rule="密码:required;password;" name="mypass" id="password" /><br /><br/>
	        <span>确认密码:</span><input type="password" data-rule="确认密码:required;match(mypass);" id="password1" /><br /><br/>
	        <span>手机号:</span><input type="text" data-rule="手机号:required;length[11]" id="mobile" /><br /><br/>
	        <span>email:</span><input type="text" data-rule="required;email;length[5~]" id="email" /><br /><br/>
	        <span>描述:</span><textarea id="description" data-rule="描述:required;length[1~, true]" rows="3" cols="50"></textarea><br/><br/>
	        <span>角色:</span>
			{{for role}}
				<input type="checkbox" data-rule="checked" id="roleCk_{{:roleId}}" value="{{:roleId}}" name="roleCkBox" />{{:roleName}}
			{{/for}}
		{{/if}}
			
	</script>
      
    </body>
</html>